package com.giscloud.limp.service.impl;

import com.alibaba.fastjson.JSON;
import com.giscloud.commons.model.Result;
import com.giscloud.datasource.page.PageInfo;
import com.giscloud.limp.dao.CaseFirstInstanceDao;
import com.giscloud.limp.dao.CaseSecondInstanceDao;
import com.giscloud.limp.dao.DefendantDao;
import com.giscloud.limp.dao.PlaintiffDao;
import com.giscloud.limp.entity.*;
import com.giscloud.limp.entity.dto.CaseSecondInstanceDTO;
import com.giscloud.limp.service.CaseSecondInstanceService;
import com.giscloud.limp.service.CaseWarnService;
import com.giscloud.token.utils.TokenUserUtil;
import com.github.pagehelper.PageHelper;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;


@Service("caseSecondInstanceService")
@Log4j2
public class CaseSecondInstanceServiceImpl implements CaseSecondInstanceService {
    private final  CaseSecondInstanceDao caseSecondInstanceDao;

    @Autowired
    private TokenUserUtil tokenUserUtil;
    //被告
    @Autowired
    private DefendantDao defendantDao;

    //原告
    @Autowired
    private PlaintiffDao plaintiffDao;

    @Autowired
    private CaseFirstInstanceDao caseFirstInstanceDao;

    @Autowired
    private CaseWarnService caseWarnService;


    @Autowired
    public CaseSecondInstanceServiceImpl(CaseSecondInstanceDao caseSecondInstanceDao) {
        this.caseSecondInstanceDao = caseSecondInstanceDao;
    }
    /**
    *
    * 列表
    */
    @Override
    public PageInfo queryPage(CaseSecondInstanceDTO caseSecondInstanceDTO) {
        log.debug("#queryPage parameter ={}", JSON.toJSONString(caseSecondInstanceDTO));
        PageHelper.startPage(caseSecondInstanceDTO.getPage(), caseSecondInstanceDTO.getRows());
        CaseSecondInstanceExample caseSecondInstanceExample = new CaseSecondInstanceExample();
        CaseSecondInstanceExample.Criteria criteria = caseSecondInstanceExample.createCriteria();
        List<CaseSecondInstance> caseSecondInstances = caseSecondInstanceDao.selectByExample(caseSecondInstanceExample);
        return PageInfo.of(caseSecondInstances);
    }


    /**
     * 信息
     */
    @Override
    public CaseSecondInstance getById(Integer caseSecondId){
        log.debug("#getById parameter ={}", caseSecondId);
        return caseSecondInstanceDao.selectByPrimaryKey(caseSecondId);
    }

    /**
     * 保存
     */
    @Override
    @Transactional
    public Result save(CaseSecondInstance caseSecondInstance){
        log.debug("#save parameter ={}", JSON.toJSONString(caseSecondInstance));
        caseSecondInstance.setCreatedStaffId(tokenUserUtil.getUserId().intValue());
        caseSecondInstanceDao.insertSelective(caseSecondInstance);
        if(CollectionUtils.isNotEmpty(caseSecondInstance.getPlaintiffs())){
            for(Plaintiff plaintiff : caseSecondInstance.getPlaintiffs()){
                plaintiff.setLitigationStage(2);
                plaintiff.setCaseId(caseSecondInstance.getCaseId());
                plaintiffDao.insertSelective(plaintiff);
            }
        }
        if(CollectionUtils.isNotEmpty(caseSecondInstance.getDefendants())){
            for(Defendant defendant : caseSecondInstance.getDefendants()){
                defendant.setLitigationStage(2);
                defendant.setCaseId(caseSecondInstance.getCaseId());
                defendantDao.insertSelective(defendant);
            }
        }
        CaseWarn caseWarn = new CaseWarn();
        caseWarn.setCaseId(caseSecondInstance.getCaseId());
        caseWarn.setLitigationStage(2);
        caseWarn.setWarnDate(caseSecondInstance.getWarnDate());
        caseWarn.setWarnContent(caseSecondInstance.getWarnContent());
        caseWarnService.save(caseWarn);
        CaseFirstInstance caseFirstInstance = caseFirstInstanceDao.selectLitigationStage(caseSecondInstance.getCaseId());
        caseFirstInstance.setCaseId(caseSecondInstance.getCaseId());
        caseFirstInstanceDao.updateByPrimaryKeySelective(caseFirstInstance);
        return Result.success("保存成功");

    }

    /**
     * 修改
     */
    @Override
    @Transactional
    public Result updateById(CaseSecondInstance caseSecondInstance){
         log.debug("#updateById parameter ={}", JSON.toJSONString(caseSecondInstance));
        caseSecondInstance.setOperateStaffId(tokenUserUtil.getUserId().intValue());
        caseSecondInstance.setVersion(new Date());
         CaseSecondInstanceExample caseSecondInstanceExample = new CaseSecondInstanceExample();
         CaseSecondInstanceExample.Criteria criteria = caseSecondInstanceExample.createCriteria();
         criteria.andCaseSecondIdEqualTo(caseSecondInstance.getCaseSecondId());
        caseSecondInstanceDao.updateByExampleSelective(caseSecondInstance,caseSecondInstanceExample);


        if(CollectionUtils.isNotEmpty(caseSecondInstance.getPlaintiffs())){
            PlaintiffExample plaintiffExample = new PlaintiffExample();
            plaintiffExample.createCriteria().andCaseIdEqualTo(caseSecondInstance.getCaseId()).andLitigationStageEqualTo(2);
            plaintiffDao.deleteByExample(plaintiffExample);
            for(Plaintiff plaintiff : caseSecondInstance.getPlaintiffs()){
                plaintiff.setLitigationStage(2);
                plaintiff.setCaseId(caseSecondInstance.getCaseId());
                plaintiffDao.insertSelective(plaintiff);
            }
        }
        if(CollectionUtils.isNotEmpty(caseSecondInstance.getDefendants())){
            DefendantExample defendantExample = new DefendantExample();
            defendantExample.createCriteria().andCaseIdEqualTo(caseSecondInstance.getCaseId()).andLitigationStageEqualTo(2);
            defendantDao.deleteByExample(defendantExample);
            for(Defendant defendant : caseSecondInstance.getDefendants()){
                defendant.setLitigationStage(2);
                defendant.setCaseId(caseSecondInstance.getCaseId());
                defendantDao.insertSelective(defendant);
            }
        }
        CaseWarn caseWarn = new CaseWarn();
        caseWarn.setCaseId(caseSecondInstance.getCaseId());
        caseWarn.setLitigationStage(2);
        caseWarn.setWarnDate(caseSecondInstance.getWarnDate());
        caseWarn.setWarnContent(caseSecondInstance.getWarnContent());
        caseWarnService.updateById(caseWarn);
        return Result.success("修改成功");
    }
    /**
     * 删除
     */
    @Override
    @Transactional
    public Result removeByIds(List<Integer> caseSecondIds){
        log.debug("#removeByIds parameter ={}", JSON.toJSONString(caseSecondIds));
        CaseSecondInstanceExample caseSecondInstanceExample = new CaseSecondInstanceExample();
        CaseSecondInstanceExample.Criteria criteria = caseSecondInstanceExample.createCriteria();
        criteria.andCaseSecondIdIn(caseSecondIds);
        List<CaseSecondInstance> caseSecondInstanceList = caseSecondInstanceDao.selectByExample(caseSecondInstanceExample);
        if(CollectionUtils.isNotEmpty(caseSecondInstanceList)){
            Set<Integer> set = new HashSet<>();
            for(CaseSecondInstance caseSecondInstance : caseSecondInstanceList){
                set.add(caseSecondInstance.getCaseId());
            }
            PlaintiffExample plaintiffExample = new PlaintiffExample();
            plaintiffExample.createCriteria().andCaseIdIn(new ArrayList<>(set));
            plaintiffDao.deleteByExample(plaintiffExample);

            DefendantExample defendantExample = new DefendantExample();
            defendantExample.createCriteria().andCaseIdIn(new ArrayList<>(set));
            defendantDao.deleteByExample(defendantExample);
        }
        caseSecondInstanceDao.deleteByExample(caseSecondInstanceExample);
        if(CollectionUtils.isNotEmpty(caseSecondInstanceList)){
            for(CaseSecondInstance caseSecondInstance : caseSecondInstanceList){
                CaseFirstInstance caseFirstInstance = caseFirstInstanceDao.selectLitigationStage(caseSecondInstance.getCaseId());
                caseFirstInstance.setCaseId(caseSecondInstance.getCaseId());
                caseFirstInstanceDao.updateByPrimaryKeySelective(caseFirstInstance);
                CaseWarn caseWarn = new CaseWarn();
                caseWarn.setCaseId(caseSecondInstance.getCaseId());
                caseWarn.setLitigationStage(2);
                caseWarnService.remove(caseWarn);
            }
        }
        return Result.success("删除成功");

    }

}